<template>
	<view class="wrap">
		<u-form style="background: #fff;padding: 0 30rpx" :model="model" :rules="rules" ref="uForm" :errorType="errorType">
			<u-form-item :leftIconStyle="{color: '#888', fontSize: '32rpx'}" left-icon="lock" label-width="200" label="原密码" prop="oldpw">
				<u-input placeholder="请输入原密码" v-model="model.oldpw" type="password"></u-input>
			</u-form-item>
			<u-form-item :leftIconStyle="{color: '#888', fontSize: '32rpx'}" label="新密码" label-width="200" prop="password" left-icon="lock">
				<u-input :password-icon="true" type="password" v-model="model.password" placeholder="请输入密码"></u-input>
			</u-form-item>
			<u-form-item :leftIconStyle="{color: '#888', fontSize: '32rpx'}" label="确认密码" label-width="200" prop="rePassword" left-icon="lock">
				<u-input type="password" v-model="model.rePassword" placeholder="请确认密码"></u-input>
			</u-form-item>
		</u-form>
		<view style="margin-top: 60rpx;">
			<u-button @click="submit" style="background-color: #fec762" :plain="false" :ripple="true" type="primary">提交</u-button>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		let that = this;
		return {
			model: {
				oldpw: '',
				password: '',
				rePassword: ''
			},
			rules: {
				oldpw:[
					{
						required: true,
						message: '请输入原密码',
						trigger: ['change','blur'],
					}
				],
				password: [
					{
						required: true,
						message: '请输入密码',
						trigger: ['change','blur'],
					},
					{
						// 正则不能含有两边的引号
						pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]+\S{5,12}$/,
						message: '需同时含有字母和数字，长度在6-12之间',
						trigger: ['change','blur'],
					}
				],
				rePassword: [
					{
						required: true,
						message: '请重新输入密码',
						trigger: ['change','blur'],
					},
					{
						validator: (rule, value, callback) => {
							return value === this.model.password;
						},
						message: '两次输入的密码不相等',
						trigger: ['change','blur'],
					}
				],
			},
			errorType: ['message'],
		};
	},
	onLoad() {

	},
	onReady() {
		this.$refs.uForm.setRules(this.rules);
	},
	methods: {
		submit() {
			this.$refs.uForm.validate(valid => {
				if (valid) {
					uni.showLoading({
						title:"正在提交",
						mask:true
					})
					this.$request.post("/member/setPw", {
						data:{
							dpw:this.model.oldpw,
							pw:this.model.password
						},
						dataType: 'json',
						responseType: 'text'
					})
					.then(res => {
						uni.hideLoading();
						this.$api.msg("修改成功");
					})
					.catch(error => {
						uni.hideLoading();
					});
				}
			});
		},
	}
};
</script>

<style scoped lang="scss">
.wrap {
	padding: 30rpx;
}

.agreement {
	display: flex;
	align-items: center;
	margin: 40rpx 0;

	.agreement-text {
		padding-left: 8rpx;
		color: $u-tips-color;
	}
}
</style>
